package cn.ydt.ydt.admin.webapi.service.impl;

import cn.ydt.ydt.admin.service.INetService;
import cn.ydt.ydt.admin.webapi.mapper.INetMapper;
import cn.ydt.ydt.admin.webapi.utils.SecurityContextUtils;
import cn.ydt.ydt.common.exception.YdtServiceException;
import cn.ydt.ydt.common.resful.StatusCode;
import cn.ydt.ydt.common.utils.FileNameUtils;
import cn.ydt.ydt.common.utils.PasswordUtils;
import cn.ydt.ydt.pojo.admin.dto.AddNetDTO;
import cn.ydt.ydt.pojo.admin.dto.UpdateNetDTO;
import cn.ydt.ydt.pojo.admin.model.Net;
import cn.ydt.ydt.pojo.admin.vo.SimpleNetLoginVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
@Slf4j
public class SimpleNetService implements INetService {

    @Autowired
    INetMapper netMapper;

    @Override
    public SimpleNetLoginVO getSimpleNetById() {
        log.info("获取当前登录的网格员的id");
        Long netId = SecurityContextUtils.getNet().getNetId();
        log.info("解析出的网格员信息={}",SecurityContextUtils.getNet().toString());
        SimpleNetLoginVO simpleNetLoginVO = netMapper.getSimpleNet(netId);
        String icon = simpleNetLoginVO.getNetIcon();
        if (icon == null){
            throw new YdtServiceException(StatusCode.BAD_REQUEST,"社区图标为空");
        }
        simpleNetLoginVO.setNetIcon(FileNameUtils.filePath(icon));
        return simpleNetLoginVO;
    }

    @Override
    public List<Net> getNetList() {
        Long communityId = SecurityContextUtils.getNetCommunityId();
        List<Net> nets = netMapper.getNetList(communityId);
        for (Net net : nets){
            String icon = net.getNetIcon();
            net.setNetIcon(FileNameUtils.filePath(icon));
        }
        return nets;
    }

    @Override
    public void addNet(AddNetDTO addNetDTO) {
        if(addNetDTO == null){
            throw new YdtServiceException(StatusCode.BAD_REQUEST,"添加管理员失败，数据为空");
        }
        Long communityId = SecurityContextUtils.getNetCommunityId();
        Net net = new Net();
        BeanUtils.copyProperties(addNetDTO,net);
        net.setCommunityId(communityId);
        LocalDateTime localDateTime = LocalDateTime.now();
        net.setCreateTime(localDateTime);
        net.setModifyTime(localDateTime);
        net.setIsManager(false);
        net.setIsDeleted(false);
        net.setIdentity((byte)2);
        String pwd = addNetDTO.getNetPassword();
        log.info("未加密的密码：{}",pwd);
        String pwds = PasswordUtils.encode(pwd);
        net.setNetPassword(pwds);
        netMapper.addNet(net);

    }

    @Override
    public Net getNetByName(String NetName) {
        if (NetName == null){
            throw new YdtServiceException(StatusCode.BAD_REQUEST,"姓名为空");
        }
        Net net = netMapper.gatNetByName(NetName);
        String icon = net.getNetIcon();
        net.setNetIcon(FileNameUtils.filePath(icon));
        return net;
    }

    @Override
    public List<String> getNameByCommunityId() {
        Long communityId = SecurityContextUtils.getNetCommunityId();
        if (communityId == null){
            throw new YdtServiceException(StatusCode.BAD_REQUEST,"社区id为空");
        }
        List<String> names = netMapper.getNameByCommunityId(communityId);
        return names;
    }

    @Override
    public void updateNet(UpdateNetDTO updateNetDTO) {
        if (updateNetDTO == null){
            throw new YdtServiceException(StatusCode.BAD_REQUEST,"需修改的数据为空");
        }
        UpdateNetDTO netDTO = new UpdateNetDTO();
        BeanUtils.copyProperties(updateNetDTO,netDTO);
        String pwd = updateNetDTO.getNetPassword();
        if (pwd == null){
            throw new YdtServiceException(StatusCode.BAD_REQUEST,"传输的密码为空");
        }
        String pwds = PasswordUtils.encode(pwd);
        netDTO.setNetPassword(pwds);
        netMapper.updateNet(netDTO);
    }
}
